import util from "../utils/util";
/**
 * 提供计算各种截止日期
 */
let calcDate={
    /**
     * 计算两个日期的月份差
     * @param date
     * @param fromDate
     * @returns {number}
     */
    calcDateMonthDifference : function (date, fromDate) {
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let fromYear = fromDate.getFullYear();
        let fromMonth = fromDate.getMonth() + 1;
        let yearToMonth = (year - fromYear) * 12;
        let monthToMonth = month - fromMonth;
        return yearToMonth + monthToMonth;
    },
    /**
     * 计算车辆年检的开始日期
     * @param registerDt
     * @param inspRule
     */
    caleInspStartDate:function(endDate,inspAdvanceMonths){
        endDate.setDate(1);
        return util.addMonths(endDate,inspAdvanceMonths+1);
    },
    /**
     * 计算车辆年检的截止日期
     * @param registerDt
     * @param inspRule
     */
    caleInspEndDate:function (registerDt,inspRule) {
        let endDate=null;
        let months = this.calcDateMonthDifference(new Date(), new Date(registerDt));
        if(months<=inspRule.months1stPeriod){
            //第一阶段逻辑
            endDate = new Date(util.addDays(new Date(util.addMonths(registerDt, inspRule.months1stPeriod+1).setDate(1)), -1));
        }else if((inspRule.months2ndPeriod && months<=inspRule.months1stPeriod+inspRule.months2ndPeriod ) || !inspRule.months3thPeriod) {
            //第二阶段
            let inspTimes=inspRule.months2ndPeriod/inspRule.secondYoi;
           for(let ii=0;ii<inspTimes;ii++){
               if(months<=inspRule.months1stPeriod+(inspRule.secondYoi*ii)){
                   endDate= util.addMonths(registerDt, inspRule.months1stPeriod+(inspRule.secondYoi*ii));
                   if(util.formatDate(endDate)<util.formatDate(new Date())){
                       endDate=new Date(util.addDays(new Date(util.addMonths(endDate,inspRule.months2ndPeriod+1).setDate(1)),-1)) ;
                   }else{
                       endDate=new Date(util.addDays(new Date(util.addMonths(endDate,1).setDate(1)),-1)) ;
                   }
                   break;
               }
           }
        }else if((inspRule.months3thPeriod && months<=inspRule.months1stPeriod+inspRule.months2ndPeriod+inspRule.months3thPeriod)|| !inspRule.months4thPeriod){
            let inspTimes=inspRule.months3thPeriod/inspRule.thirdYoi;
            for(let ii=0;ii<inspTimes;ii++){
                if(months<=inspRule.months1stPeriod+inspRule.months2ndPeriod+(inspRule.thirdYoi*ii)){
                    endDate= util.addMonths(registerDt, inspRule.months1stPeriod+inspRule.months2ndPeriod+(inspRule.thirdYoi*ii));
                    if(util.formatDate(endDate)<util.formatDate(new Date())){
                        endDate=new Date(util.addDays(new Date(util.addMonths(endDate,inspRule.months3thPeriod+1).setDate(1)),-1)) ;
                    }else{
                        endDate=new Date(util.addDays(new Date(util.addMonths(endDate,1).setDate(1)),-1)) ;
                    }
                    break;
                }
            }
        }else if(inspRule.months4thPeriod){
            let longTime= inspRule.months4thPeriod-inspRule.months3thPeriod-inspRule.months2ndPeriod-inspRule.months1stPeriod;
            let inspTimes=Math.ceil(longTime/inspRule.fourthYoi);
            for(let ii=0;ii<inspTimes;ii++){
                if(months<=inspRule.months1stPeriod+inspRule.months2ndPeriod + inspRule.months3thPeriod +(inspRule.secondYoi*ii)){
                    let totalMonths=inspRule.months1stPeriod+inspRule.months2ndPeriod+ inspRule.months3thPeriod+(inspRule.fourthYoi*ii);
                    endDate= util.addMonths(registerDt, totalMonths);
                    if(util.formatDate(endDate)<util.formatDate(new Date())){
                        endDate=new Date(util.addDays(new Date(util.addMonths(endDate,inspRule.months4thPeriod+1).setDate(1)),-1)) ;
                    }else{
                        endDate=new Date(util.addDays(new Date(util.addMonths(endDate,1).setDate(1)),-1)) ;
                    }
                    break;
                }
            }
        }

        return endDate;

    },
    /**
     * 计算日期的时间差,返回相差的天数
     * @param date
     * @param fromDate
     * @returns {number}
     */
    calcDateDayDifference : function (date, fromDate) {
        let dateSpan = Math.abs(date - fromDate);
        return Math.floor(dateSpan / (24 * 3600 * 1000));
    }
};


export default calcDate;